package com.carey.blog.libs.google.auth;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.carey.blog.model.Preference;
import com.carey.blog.util.Preferences;

/**
 * OAuth callback.
 * 
 */
public final class BuzzOAuthCallback extends HttpServlet {

	/**
	 * Default serial version uid.
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Logger.
	 */
	private static final Logger LOGGER = Logger
			.getLogger(BuzzOAuthCallback.class.getName());
	/**
	 * Sleep interval in milliseconds.
	 */
	public static final long SLEEP_INTERVAL = 3000;
	/**
	 * Preference utilities.
	 */
	private Preferences preferenceUtils = Preferences.getInstance();
	/**
	 * OAuth utilities.
	 */
	private OAuths oAuths = OAuths.getInstance();
	/**
	 * Key of Google Buzz token.
	 */
	public static final String GOOGLE_BUZZ_TOKEN = "googleBuzzToken";
	/**
	 * Key of Google Buzz verifier.
	 */
	public static final String GOOGLE_BUZZ_VERIFIER = "googleBuzzVerifier";

	@Override
	protected void doGet(final HttpServletRequest request,
			final HttpServletResponse response) throws ServletException,
			IOException {
		final String requestToken = request.getParameter("oauth_token");
		final String verifier = request.getParameter("oauth_verifier");
		LOGGER.log(Level.FINE,
				"OAuth callback from Google[requestToken={0}, verifier={1}",
				new String[] { requestToken, verifier });

		// try {
		// oAuths.sign(requestToken, verifier, BuzzOAuth.getHttpTransport());
		// } catch (final Exception e) {
		// LOGGER.log(Level.SEVERE, e.getMessage(), e);
		// throw new ServletException(e);
		// }

		try {
			final JSONObject preference = preferenceUtils.getPreference();
			preference.put(GOOGLE_BUZZ_TOKEN, requestToken);
			preference.put(GOOGLE_BUZZ_VERIFIER, verifier);

			final JSONObject requestJSONObject = new JSONObject();
			requestJSONObject.put(Preference.PREFERENCE, preference);

			preferenceUtils.setPreference(preference);
		} catch (final Exception e) {
			LOGGER.log(Level.SEVERE, e.getMessage(), e);
			throw new ServletException(e);
		}

		response.sendRedirect("/admin-index.do");
	}
}
